package com.situ.hospital.doctor.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.situ.hospital.doctor.model.DoctorModel;
import com.situ.hospital.doctor.service.DoctorService;

import util.IsEmpty;

/**
 * Servlet implementation class DoctorServlet
 */
@WebServlet("/DoctorServlet")
public class DoctorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DoctorService service = new DoctorService();

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		switch (action) {
		case "page":
			page(request, response);
			break;
		case "add":
			add(request, response);
			break;
		case "selectmodel":
			selectmodel(request, response);
			break;
		case "upd":
			upd(request, response);
			break;
		case "updjpg":
			updjpg(request, response);
			break;
		case "del":
			del(request, response);
			break;
		}
	}

	private void updjpg(HttpServletRequest request, HttpServletResponse response) {
		String updjpgid = request.getParameter("doctorid");
		if (!IsEmpty.isEmpty(updjpgid)) {
			DoctorModel doctor = new DoctorModel();
			doctor.setDoctorId(Integer.parseInt(updjpgid));
			try {
				response.getWriter().write(String.valueOf(service.updatejpg(doctor)));
			} catch (IOException | SQLException e) {
				e.printStackTrace();
			}
		} else {
			try {
				response.getWriter().write("2");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	private void del(HttpServletRequest request, HttpServletResponse response) {
		String delid = request.getParameter("delid");
		DoctorModel doctor = new DoctorModel();
		doctor.setDoctorId(Integer.parseInt(delid));
		try {
			response.getWriter().write(String.valueOf(service.del(doctor)));
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
	}

	private void upd(HttpServletRequest request, HttpServletResponse response) {
		String updid = request.getParameter("doctorid");
		String updname = request.getParameter("doctorname");
		String updage = request.getParameter("doctorage");
		String updtitle = request.getParameter("doctortitle");
		String upddepartmentname = request.getParameter("departmentname");
		if (!IsEmpty.isEmpty(updid) && !IsEmpty.isEmpty(updname) && !IsEmpty.isEmpty(updage)
				&& !IsEmpty.isEmpty(updtitle) && !IsEmpty.isEmpty(upddepartmentname)) {
			DoctorModel upddoctor = new DoctorModel(Integer.parseInt(updid), updname, updage, updtitle,
					upddepartmentname);
			try {
				response.getWriter().write(String.valueOf(service.update(upddoctor)));
			} catch (IOException | SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private void selectmodel(HttpServletRequest request, HttpServletResponse response) {
		String selectid = request.getParameter("doctorid");
		DoctorModel doctor = new DoctorModel();
		doctor.setDoctorId(Integer.parseInt(selectid));
//		System.out.println(selectid);
		try {
			JSONObject jso = new JSONObject(service.selectModel(doctor));
			response.getWriter().print(jso);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		String addid = request.getParameter("doctorid");
		String addname = request.getParameter("doctorname");
		String addage = request.getParameter("doctorage");
		String addtitle = request.getParameter("doctortitle");
		String adddepartmentname = request.getParameter("departmentname");
		if (!IsEmpty.isEmpty(addid) && !IsEmpty.isEmpty(addname) && !IsEmpty.isEmpty(addage)
				&& !IsEmpty.isEmpty(addtitle) && !IsEmpty.isEmpty(adddepartmentname)) {
			DoctorModel adddoctor = new DoctorModel(Integer.parseInt(addid), addname, addage, addtitle,
					adddepartmentname);
			try {
				response.getWriter().write(String.valueOf(service.add(adddoctor)));
			} catch (IOException | SQLException e) {
				e.printStackTrace();
			}
		} else {
			try {
				response.getWriter().write("2");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	private void page(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		String name = request.getParameter("doctorname");
		String age = request.getParameter("doctorage");
		String title = request.getParameter("doctortitle");
		String departmentname = request.getParameter("departmentname");
		String limit = request.getParameter("limit");
		String curr = request.getParameter("curr");
//		System.out.println(name);
		DoctorModel doctor = new DoctorModel();
		doctor.setDoctorName(name);
		doctor.setDoctorAge(age);
		doctor.setDoctorTitle(title);
		doctor.setDepartmentName(departmentname);

		doctor.setPageLimit(Integer.parseInt(limit));
		doctor.setPageIndex(Integer.parseInt(curr));
		doctor.setPageOn(true);

		try {
			map.put("list", service.select(doctor));
			map.put("count", service.selectCount(doctor));
			JSONObject jso = new JSONObject(map);
			response.getWriter().print(jso);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}

	}

}
